package cn.usian.cart.controller;

import cn.usian.cart.service.CartService;
import cn.usian.jyzx.model.entity.h5.CartInfo;
import cn.usian.jyzx.model.vo.common.Result;
import cn.usian.jyzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.PushBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order/cart")
@Tag(name = "购物车接口")
public class CartController {
    @Autowired
    CartService cartService;

    @GetMapping("/auth/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Integer skuId, @PathVariable Integer skuNum){
//        String token = request.getHeader("token");

        cartService.addToCart(skuId,skuNum);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/auth/cartList")
    public Result<List<CartInfo>> carList(){
//        String token = request.getHeader("token");
        List<CartInfo> cartInfos = cartService.carList();
        return Result.build(cartInfos,ResultCodeEnum.SUCCESS);
    }
    @DeleteMapping("auth/deleteCart/{skuId}")
    @Operation(summary = "删除购物车商品")
    public Result deleteCart(@PathVariable Integer skuId){
//        String token = request.getHeader("token");
        cartService.deleteCart(skuId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

//    更新选中商品的状态
    @Operation(summary="更新购物车商品选中状态")
    @GetMapping("/auth/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Integer skuId,@PathVariable Integer isChecked){
//        String token = request.getHeader("token");
        cartService.checkCart(skuId,isChecked);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

//    购物车全选全不选
    @Operation(summary="更新购物车商品全部选中状态")
    @GetMapping("/auth/allCheckCart/{isChecked}")
    public Result allCheckCart(@PathVariable Integer isChecked){
//        String token = request.getHeader("token");
        cartService.allCheckCart(isChecked);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

//    清空购物车

    @Operation(summary="清空购物车")
    @GetMapping("/auth/clearCart")
    public Result clearCart(){
//        String token = request.getHeader("token");
        cartService.clearCart();
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/auth/cartLists")
    public List<CartInfo> cartLists(){
//        String token = request.getHeader("token");
        List<CartInfo> cartInfos = cartService.carList();
        return cartInfos;
    }

}
